package org.fpdev.apps.cart.network;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Stroke;

public class SubStyle {

  private int width_;
  private Color color_;
  private float[] dash_;

  public SubStyle(int width, Color color) {
    this(width, color, null);
  }

  public SubStyle(int width, Color color, float[] dash) {
    super();
    width_ = width;
    color_ = color;
    dash_ = dash;
  }

  public Stroke getStroke() {
    return new BasicStroke(width_, BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND, 1, dash_, 0);
  }

  public Color getColor() {
    return color_;
  }

  public void setColor(Color color) {
    color_ = color;
  }

  public int getWidth() {
    return width_;
  }

  public void setWidth(int width) {
    width_ = width;
  }

  public static float[] parseDash(String dash) throws NumberFormatException {
    String strArr[] = dash.split(",");
    float dashArr[] = new float[strArr.length];
    for(int i=0; i < strArr.length; i++) {
      dashArr[i] = Float.parseFloat(strArr[i]);
    }
    return dashArr;
  }

  public boolean setDash(String dash) {
    try {
      setDash(parseDash(dash));
      return true;
    }
    catch(NumberFormatException ex) {
      return false;
    }
  }

  public void setDash(float[] dash) {
    dash_ = dash;
  }

  public float[] getDash() {
    return dash_;
  }

  public String getDashStr() {
    if(dash_ == null) return "";
    String dashStr = "";
    for(int i = 0; i < dash_.length; i++) {
      dashStr += dash_[i] + (i < dash_.length-1 ? "," : "");
    }
    return dashStr;
  }

}
